import 'package:get/get.dart';
import 'package:xinxuan/common/dialogs/message_dialog.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/utils/color_util.dart';
import 'package:xinxuan/vo/blindbox/blindbox_order_list_vo.dart';
import 'package:xinxuan/vo/common_vo/common_pagination_vo.dart';
import 'package:xinxuan/vo/msg_vo.dart';

class BlindBoxOrderPageController extends GetxController {

  final CommonPageController commonPageController = Get.find();

  /// tab 下标
  var tabIndex = 0.obs;

  List<int> statusArr = [-1, 2, 6, 8, 10];
  List<String> statusTypeStr = ['全部', '待提货', '已转提', '已自提', '已核销'];
  /// 订单数据
  var dataArr = <BlindBoxOrderListVo>[
    BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
    BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
    BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
    BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
    BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
  ].obs;


  @override
  void onInit() {
    Map<String, dynamic> argumentsMap = Get.arguments;
    tabIndex.value = argumentsMap['tabIndex'] ?? 0;
    if (commonPageController.userData.value.isWriteoffOfficer != 1) {
      statusArr = [-1, 2, 6, 8];
      statusTypeStr = ['全部', '待提货', '已转提', '已自提'];
      /// 订单数据
      dataArr = <BlindBoxOrderListVo>[
        BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
        BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
        BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1))),
        BlindBoxOrderListVo(data: [], meta: BlindBoxOrderMeta(pagination: CommonPagination(total: 0, currentPage: 1)))
      ].obs;
    }
    update();
    super.onInit();
  }

  
  @override
  void onReady() {
    getBlindBoxOrderList();
    super.onReady();
  }

  getBlindBoxOrderList() async {
    dataArr[tabIndex.value].meta!.pagination!.currentPage = 1;
    BlindBoxOrderListVo tempVo = await HttpUtilService.getBlindBoxOrderList(status: statusArr[tabIndex.value], page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, loading: true);
    dataArr[tabIndex.value] = tempVo;
    update();
  }

  /// 加载更多订单列表
  onloadBlindBoxOrderList() async {    
    if (dataArr[tabIndex.value].meta!.pagination!.total! <= dataArr[tabIndex.value].data!.length) {
      return;
    }
    dataArr[tabIndex.value].meta!.pagination!.currentPage = dataArr[tabIndex.value].meta!.pagination!.currentPage! + 1;
    BlindBoxOrderListVo tempVo = await HttpUtilService.getBlindBoxOrderList(status: statusArr[tabIndex.value], page: dataArr[tabIndex.value].meta!.pagination!.currentPage!, loading: false);
    dataArr[tabIndex.value].data!.addAll(tempVo.data!);
    update();
  }

  /// 获取Tab某项订单列表
  getBlindBoxTabItemOrderList(int index) async {
    dataArr[index].meta!.pagination!.currentPage = 1;
    BlindBoxOrderListVo tempVo = await HttpUtilService.getBlindBoxOrderList(status: statusArr[index], page: dataArr[index].meta!.pagination!.currentPage!, loading: false);
    dataArr[index] = tempVo;
    update();
  }

  /// 核销
  Future<bool> checkoff(int orderId) async {
    MsgVo? msgVo = await HttpUtilService.checkoff(id: orderId);
    if (msgVo != null) {
      Get.dialog(MessageDialog(
        message: msgVo.data!.msg ?? '提示异常', 
        title: '提示信息',
        messageColor: textSubtitleColor,
      ));
      return false;
    } else {
      getBlindBoxTabItemOrderList(tabIndex.value);
      return true;
    }
  }


}